package org.scohen.juploadr.uploadapi.flickrrest.photosets;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

import org.scohen.juploadr.app.ImageAttributes;
import org.scohen.juploadr.app.PhotoSet;

public class FlickrPhotoSet implements Comparable<PhotoSet>, PhotoSet {

    private String id;
    private String secret;
    private String server;
    private int numberOfPhotos;
    private String title;
    private String description;
    private ImageAttributes primaryPhoto;
    private String primaryPhotoId;
    private boolean New = true;
    private List<ImageAttributes> photos = new ArrayList<ImageAttributes>();
    private String url;

    public FlickrPhotoSet() {
    }

    public FlickrPhotoSet(String name, String description, ImageAttributes primary) {
        this.title = name;
        this.description = description;
        this.primaryPhoto = primary;
        addPhoto(primary);

    }

    /*
     * (non-Javadoc)
     * 
     * @see org.scohen.juploadr.uploadapi.flickrrest.photosets.PhotoSet#getDescription()
     */
    public String getDescription() {
        return description;
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.scohen.juploadr.uploadapi.flickrrest.photosets.PhotoSet#setDescription(java.lang.String)
     */
    public void setDescription(String description) {
        this.description = description;
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.scohen.juploadr.uploadapi.flickrrest.photosets.PhotoSet#getId()
     */
    public String getId() {
        return id;
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.scohen.juploadr.uploadapi.flickrrest.photosets.PhotoSet#setId(java.lang.String)
     */
    public void setId(String id) {
        this.id = id;
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.scohen.juploadr.uploadapi.flickrrest.photosets.PhotoSet#getNumberOfPhotos()
     */
    public int getNumberOfPhotos() {
        return numberOfPhotos;
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.scohen.juploadr.uploadapi.flickrrest.photosets.PhotoSet#setNumberOfPhotos(int)
     */
    public void setNumberOfPhotos(int numberOfPhotos) {
        this.numberOfPhotos = numberOfPhotos;
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.scohen.juploadr.uploadapi.flickrrest.photosets.PhotoSet#getSecret()
     */
    public String getSecret() {
        return secret;
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.scohen.juploadr.uploadapi.flickrrest.photosets.PhotoSet#setSecret(java.lang.String)
     */
    public void setSecret(String secret) {
        this.secret = secret;
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.scohen.juploadr.uploadapi.flickrrest.photosets.PhotoSet#getServer()
     */
    public String getServer() {
        return server;
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.scohen.juploadr.uploadapi.flickrrest.photosets.PhotoSet#setServer(java.lang.String)
     */
    public void setServer(String server) {
        this.server = server;
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.scohen.juploadr.uploadapi.flickrrest.photosets.PhotoSet#getTitle()
     */
    public String getTitle() {
        return title;
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.scohen.juploadr.uploadapi.flickrrest.photosets.PhotoSet#setTitle(java.lang.String)
     */
    public void setTitle(String title) {
        this.title = title;
    }

    public class TitleComparator implements Comparator {
        public TitleComparator() {
        }

        public int compare(Object o1, Object o2) {
            PhotoSet p1 = (PhotoSet) o1;
            PhotoSet p2 = (PhotoSet) o2;
            return p1.getTitle().compareTo(p2.getTitle());
        }

    }

    /*
     * (non-Javadoc)
     * 
     * @see java.lang.Object#equals(java.lang.Object)
     */
    /*
     * (non-Javadoc)
     * 
     * @see org.scohen.juploadr.uploadapi.flickrrest.photosets.PhotoSet#equals(java.lang.Object)
     */
    public boolean equals(Object o) {
        if (o instanceof FlickrPhotoSet) {
            PhotoSet p = (PhotoSet) o;
            if (id != null) {
                return id.equals(p.getId());
            } else {
                return title.equals(p.getTitle());
            }
        }
        return false;
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.scohen.juploadr.uploadapi.flickrrest.photosets.PhotoSet#isNew()
     */
    public boolean isNew() {
        return New;
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.scohen.juploadr.uploadapi.flickrrest.photosets.PhotoSet#setNew(boolean)
     */
    public void setNew(boolean new1) {
        New = new1;
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.scohen.juploadr.uploadapi.flickrrest.photosets.PhotoSet#getPrimaryPhoto()
     */
    public ImageAttributes getPrimaryPhoto() {
        return primaryPhoto;
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.scohen.juploadr.uploadapi.flickrrest.photosets.PhotoSet#setPrimaryPhoto(org.scohen.juploadr.app.ImageAttributes)
     */
    public void setPrimaryPhoto(ImageAttributes primaryPhoto) {
        this.primaryPhoto = primaryPhoto;
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.scohen.juploadr.uploadapi.flickrrest.photosets.PhotoSet#getPrimaryPhotoId()
     */
    public String getPrimaryPhotoId() {
        if (primaryPhoto != null && primaryPhoto.getPhotoId() != null) {
            return primaryPhoto.getPhotoId();

        }
        return primaryPhotoId;
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.scohen.juploadr.uploadapi.flickrrest.photosets.PhotoSet#setPrimaryPhotoId(java.lang.String)
     */
    public void setPrimaryPhotoId(String primaryPhotoId) {
        this.primaryPhotoId = primaryPhotoId;
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.scohen.juploadr.uploadapi.flickrrest.photosets.PhotoSet#getPhotos()
     */
    public List<ImageAttributes> getPhotos() {
        return photos;
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.scohen.juploadr.uploadapi.flickrrest.photosets.PhotoSet#addPhoto(org.scohen.juploadr.app.ImageAttributes)
     */
    public void addPhoto(ImageAttributes atts) {
        if (!photos.contains(atts)) {
            atts.addPhotoSet(this);
            photos.add(atts);
        }
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.scohen.juploadr.uploadapi.flickrrest.photosets.PhotoSet#removePhoto(org.scohen.juploadr.app.ImageAttributes)
     */
    public void removePhoto(ImageAttributes atts) {
        if (photos.contains(atts)) {
            photos.remove(atts);
            atts.removePhotoSet(this);
        }
    }

    public boolean hasPhoto(ImageAttributes photo) {
        return photos.contains(photo);
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.scohen.juploadr.uploadapi.flickrrest.photosets.PhotoSet#getUrl()
     */
    public String getUrl() {
        return url;
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.scohen.juploadr.uploadapi.flickrrest.photosets.PhotoSet#setUrl(java.lang.String)
     */
    public void setUrl(String url) {
        this.url = url;
    }

    public int compareTo(PhotoSet p1) {
        return title.compareToIgnoreCase(p1.getTitle());
    }

    @Override
    public String toString() {
        return new StringBuffer("FlickrPhotoSet").append(" ").append(getTitle()).append(" new?").append(isNew())
                .toString();
    }

}
